package cn.topevery.garden.dal.dao.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.PlanDao;
import cn.topevery.garden.dal.dao.PlanFileDao;
import cn.topevery.garden.dal.util.QueryWrapper;
import cn.topevery.garden.entity.dto.ScheduleDTO;
import cn.topevery.garden.entity.po.Plan;
import cn.topevery.garden.entity.query.PlanQuery;
import cn.topevery.garden.entity.vo.PestAnalyzeVO;
import cn.topevery.garden.entity.vo.PlanVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.DbScriptHelper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.entity.Value;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;


/**
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class PlanDaoImpl extends BaseDaoImpl<Plan> implements PlanDao {

    @Autowired
    PlanFileDao planFileDao;

    @Autowired
    PlanFileDaoImpl planFileDaoImpl;

    @Override
    public <P extends DbCsp> List<Plan> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<PlanVO> listPage(PlanQuery query) {
        //deptId请改为从controller层获取
        //SysDept dept = getDept();
        //System.out.println("dept.deptId = " + dept.deptId);
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;
        Map<String, Object> map = new HashMap<>();
        map.put("beginDate", query.getBeginDate());
        map.put("endDate", query.getEndDate());
//        if (!dept.deptId.equals("835B6E1F-D9E7-42DB-A362-1317D8DBF8FB")) {
//            map.put("deptId", dept.deptId);
//        }
        map.put("isApprover", query.getIsApprover());
        map.put("keyWord", query.getKeyWord());
        StringBuilder sqlBuilder = new StringBuilder("(SELECT\n" +
                "        tp.c_name,\n" +
                "        c_section_name,\n" +
                "        c_company_name,\n" +
                "        c_company_id,\n" +
                "        GROUP_CONCAT( tp.c_maintain_type_name SEPARATOR ',' ) AS c_maintain_type_name,\n" +
                "        tp.c_db_create_date,\n" +
                "        c_start_date,\n" +
                "        c_end_date,\n" +
                "        c_plan_id,\n" +
                "        c_send,\n" +
                "        max(c_state) c_state,\n" +
                "        tsu.c_user_name `c_db_create_name`,\n" +
                "        tp.c_obj_type,\n" +
                "        (select count(1) from t_plan t where t.c_plan_id = tp.c_plan_id and t.c_plan_state != 2 and t.c_db_status = 0) c_count,\n" +
                "        COUNT( c_plan_id) c_counts\n" +
                "        FROM t_plan tp\n" +
                "        JOIN t_sys_user tsu ON tp.c_db_create_id = tsu.c_id\n" +
                "where tp.c_db_status=0 ");
        if (ObjectUtil.isNotEmpty(map.get("deptId"))) {
            sqlBuilder.append("IF (c_state < 2 ,  tp.c_company_id=#{map.deptId}, tp.c_company_id='" + map.get("deptId") + "'\n" +
                    "            OR (tp.c_company_id in (SELECT c_dept_id  FROM t_sys_dept WHERE c_dept_pid = '" + map.get("deptId") + "'  )\n" +
                    "            OR tp.c_company_id in (SELECT c_dept_id FROM t_sys_dept WHERE c_dept_pid IN  (SELECT c_dept_id  FROM t_sys_dept WHERE c_dept_pid = '" + map.get("deptId") + "'  ))\n" +
                    "            OR tp.c_company_id in (SELECT c_dept_id FROM t_sys_dept WHERE c_dept_pid IN (SELECT c_dept_id FROM t_sys_dept WHERE c_dept_pid IN  (SELECT c_dept_id  FROM t_sys_dept WHERE c_dept_pid = '" + map.get("deptId") + "'  ))))\n" +
                    "            )");
        }
        if (ObjectUtil.isNotEmpty(map.get("beginDate")) && ObjectUtil.isNotEmpty(map.get("endDate"))) {
            sqlBuilder.append(" and tp.c_db_create_date between '" + map.get("beginDate") + "' and '" + map.get("endDate") + "'");
        }

        if (StringUtils.isNotEmpty(query.getDate())) {
            sqlBuilder.append(" and '" + query.getDate() + "' between tp.c_start_date and tp.c_start_date ");
        }

        if (query.getSend() != null && query.getSend() == 1) {
            sqlBuilder.append(" and tp.c_send =1");
        }
        sqlBuilder.append(
                "            GROUP BY\n" +
                        "            tp.c_name,\n" +
                        "        c_section_name,\n" +
                        "            c_company_name,\n" +
                        "            c_company_id,\n" +
                        "            c_plan_id,\n" +
                        "            c_start_date,\n" +
                        "            c_end_date,\n" +
                        "        c_send,\n" +
                        "            tp.c_db_create_date,\n" +
                        "            tsu.c_user_name,\n" +
                        "            tp.c_obj_type");
        if (ObjectUtil.isNotEmpty(map.get("keyWord"))) {
            sqlBuilder.append(" HAVING\n" +
                    "                CONCAT(\n" +
                    "                IFNULL( c_name, '' ),\n" +
                    "                IFNULL( CASE c_state WHEN 1 THEN '未审核' WHEN 2 THEN '执行中' ELSE '结束' END, '' ),\n" +
                    "                IFNULL( c_company_name, '' ),\n" +
                    "                IFNULL( c_maintain_type_name, '' )) like concat ('%','" + map.get("keyWord") + "','%')");
        }
        sqlBuilder.append(" order by tp.c_db_create_date desc) t");
        String sql = DbScriptHelper.getSelectSqlPage(sqlBuilder.toString(), null, null, null, pageSize, pageIndex);
        DataPager<PlanVO> dataPagerByCustomize = getDataPagerByCustomize(sql, CommandType.Text, null, PlanVO.class);
        return dataPagerByCustomize;
    }

    @Override
    public Plan getEntityByColumn(String key, String value) {
        KeyValue<String> map = new KeyValue<>();
        map.key = key;
        map.value = value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public void commit(PlanVO vo, String userId) {
        Plan entity = new Plan();
        BeanUtils.copyProperties(vo, entity);
        String name = entity.getName();
        if (vo.getPlanId() == null) {
            entity.setPlanId(UUID.randomUUID().toString());
        }
        for (Plan plan : vo.getPlanList()) {
            entity.setName(name + '(' + entity.getStartDate().toString().substring(0, 10) + ')');
            entity.setObjType(plan.getObjType());
            entity.setContent(plan.getContent());
            entity.setObjId(plan.getObjId());
            entity.setObjName(plan.getObjName());
            entity.setNumber(plan.getNumber());
            entity.setUnit(plan.getUnit());
            entity.setKind(plan.getKind());
            entity.setPlanEndDate(plan.getPlanEndDate());
            entity.setPlanStartDate(plan.getPlanStartDate());
            entity.setMaintainType(plan.getMaintainType());
            entity.setMaintainTypeName(plan.getMaintainTypeName());
            entity.setState(1);
            entity.setPlanState(1);
            entity.setDbStatus(false);
            entity.setSend(false);
            if (plan.getId() == null) {
                saveOrUpdate(userId, entity);
                if (vo.getProtect() == 2 && ObjectUtil.isNotEmpty(vo.getDate())) {
                    while (vo.getDate().isAfter(entity.getStartDate().toLocalDate())) {
                        entity.setPlanId(UUID.randomUUID().toString());
                        entity.setStartDate(entity.getStartDate().plusWeeks(1));
                        entity.setEndDate(entity.getEndDate().plusWeeks(1));
                        entity.setName(name + '(' + entity.getStartDate().toString().substring(0, 10) + ')');
                        saveOrUpdate(userId, entity);
                    }
                }
                if (vo.getProtect() == 3 && ObjectUtil.isNotEmpty(vo.getDate())) {
                    while (vo.getDate().isAfter(entity.getStartDate().toLocalDate())) {
                        entity.setPlanId(UUID.randomUUID().toString());
                        entity.setStartDate(entity.getStartDate().plusMonths(1));
                        entity.setEndDate(entity.getEndDate().plusMonths(1));
                        entity.setName(name + '(' + entity.getStartDate().toString().substring(0, 10) + ')');
                        saveOrUpdate(userId, entity);
                    }
                }
            } else {
                //如果是修改，先删除旧的附件，再把新的附件保存
                SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_plan_file");
                sqlUpdateMate.setColumnValue(true, "c_db_status", 0);
                sqlUpdateMate.where(StringUtils.isNotEmpty(entity.getPlanId()), "c_plan_id", ScriptConditionEnum.Eq, entity.getPlanId());
                run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
                    sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
                    dbCommand.executeNonQuery();
                });
                entity.setId(plan.getId());
                entity.setName(name);
                update(userId, entity);
            }
        }
        planFileDaoImpl.updateByTable(userId, new QueryWrapper<KeyValue>().eq("c_db_status", 1),
                new QueryWrapper<KeyValue>().where("c_plan_id", entity.getPlanId()).where("c_data_type", 1));
        if (vo.getFiles() != null && !vo.getFiles().isEmpty()) {
            planFileDao.insert(userId, vo.getFiles().stream().peek(temp -> {
                temp.setDataType(1);
                temp.setPlanId(entity.getPlanId());
                temp.setPlanIntId(entity.getId());
                temp.setDbStatus(false);
                temp.setDbUpdateId(userId);
                temp.setDbUpdateDate(LocalDateTime.now());
            }).collect(Collectors.toList()));
        }
    }

    @Override
    public DataPager<PlanVO> getList(DataPager<PlanVO> page, Map<String, Object> map) {
        return null;
    }

    @Override
    public void deleteByIds(String planId, String userId) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_plan");
        sqlUpdateMate.setColumnValue(true, "c_db_status", 1)
                .setColumnValue(StringUtils.isNotEmpty(userId), "c_db_update_id", userId)
                .setColumnValue(true, "c_db_update_date", LocalDateTime.now());
        sqlUpdateMate.where(StringUtils.isNotEmpty(planId), "c_plan_id", ScriptConditionEnum.Eq, planId);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public List<Plan> getByName(String name) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_plan");
        sqlQueryMate.where(StringUtils.isNotEmpty(name), "c_plan_id", ScriptConditionEnum.Eq, name);
        return selectList(sqlQueryMate, Plan.class);
    }

    @Override
    public PlanVO getGroupByName(String name) {
        String table = "t_plan tp\n" +
                "        JOIN t_sys_user tsu ON tp.c_db_create_id = tsu.c_id";
        String columns = "tp.c_name,\n" +
                "        c_company_name,\n" +
                "        c_company_id,\n" +
                "        c_section_id,\n" +
//                "        c_protect,\n" +

                "        c_section_name,\n" +
                "        GROUP_CONCAT( tp.c_maintain_type_name ORDER BY tp.c_maintain_type_name SEPARATOR ',' ) AS c_maintain_type_name,\n" +
                "        tp.c_db_create_date,\n" +
                "        c_start_date,\n" +
                "        c_end_date,\n" +
                "        tsu.c_user_name `c_db_create_name`,\n" +
                "        c_plan_id,\n" +
                "        max(c_state) c_state,\n" +
                "        tp.c_remark";
        String groupBy = "tp.c_name,\n" +
                "            c_company_name,\n" +
                "            c_company_id,\n" +
                "            c_start_date,\n" +
                "        c_section_id,\n" +
                "        c_section_name,\n" +
//                "        c_protect,\n" +

                "            c_end_date,\n" +
                "            tp.c_db_create_date,\n" +
                "            tsu.c_user_name,\n" +
                "            c_plan_id,\n" +
                "            tp.c_remark";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate.where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotEmpty(name), "tp.c_plan_id", ScriptConditionEnum.Eq, name);
        sqlQueryMate.setGroupBy(groupBy);
        return selectOne(sqlQueryMate, PlanVO.class);
    }

    @Override
    public List<Plan> getPestPlanList() {
        String table = "t_plan as t";
        String columns = "t.*, (select map.c_name from t_sys_map map where map.c_map_id = t.c_map_id) as mapName";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate.where(true, "t.c_maintain_type", ScriptConditionEnum.Eq, "d10e395c-e9b4-4d63-adeb-623a15301943")
                .where(true, "t.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "t.c_state", ScriptConditionEnum.NotIn, 1);
        return selectList(sqlQueryMate, Plan.class);
    }

    @Transactional
    @Override
    public void mainPlanApprove(String planId, String userId) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_plan");
        sqlUpdateMate.setColumnValue(true, "c_state", 2)
                .setColumnValue(StringUtils.isNotEmpty(userId), "c_db_update_id", userId)
                .setColumnValue(true, "c_db_update_date", LocalDateTime.now());
        sqlUpdateMate.where(StringUtils.isNotEmpty(planId), "c_plan_id", ScriptConditionEnum.Eq, planId)
                .where(true, "c_db_status", ScriptConditionEnum.Eq, 0);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Transactional
    @Override
    public boolean mainPlanConfirm(String planId, String userId) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_plan").setColumns("count(1)");
        sqlQueryMate.where(StringUtils.isNotEmpty(planId), "c_plan_id", ScriptConditionEnum.Eq, planId)
                .where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "c_state", ScriptConditionEnum.NotEq, 2);
        Integer count = selectOne(sqlQueryMate, Integer.class);
        if (count == 0) {
            SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_plan");
            sqlUpdateMate.setColumnValue(true, "c_state", 3)
                    .setColumnValue(StringUtils.isNotEmpty(userId), "c_db_update_id", userId)
                    .setColumnValue(true, "c_db_update_date", LocalDateTime.now());
            sqlUpdateMate.where(StringUtils.isNotEmpty(planId), "c_plan_id", ScriptConditionEnum.Eq, planId)
                    .where(true, "c_db_status", ScriptConditionEnum.Eq, 0);
            run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
                sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
                dbCommand.executeNonQuery();
            });
            return true;
        }
        return false;
    }

    @Override
    public void subPlanExecute(PlanVO vo, String userId) {
        LocalDateTime now = LocalDateTime.now();
        Plan entity = new Plan();
        BeanUtils.copyProperties(vo, entity);
        entity.setPlanState(2);
        entity.setDbUpdateDate(now);
        entity.setDbUpdateId(userId);
        update(userId, entity);
        if (vo.getFiles() != null && !vo.getFiles().isEmpty()) {
            planFileDao.insert(userId, vo.getFiles().stream().peek(temp -> {
                temp.setDataType(2);
                temp.setCreateTime(now);
                temp.setPlanIntId(vo.getId());
                temp.setDbStatus(false);
                temp.setCreateId(userId);
                temp.setDbUpdateDate(now);
                temp.setDbUpdateId(userId);
            }).collect(Collectors.toList()));
        }
    }

    @Override
    public List<PestAnalyzeVO> getPestDeptInfo(boolean isSum) {
        String sql = "SELECT\n" +
                "        dept.c_dept_id as deptId,\n" +
                "        dept.c_dept_name as deptName,\n" +
                "        IFNULL( number.num, 0 ) AS number\n" +
                "        FROM\n" +
                "        (\n" +
                "        SELECT\n" +
                "        b.c_dept_id,\n" +
                "        b.c_dept_name\n" +
                "        FROM\n" +
                "        t_sys_dept_in_tree a\n" +
                "        JOIN t_sys_dept b ON a.c_dept_id = b.c_dept_id\n" +
                "        AND a.c_db_status = 0\n" +
                "        AND b.c_dept_type = '15003'\n" +
                "        WHERE\n" +
                "        c_dept_tree_id = '55003f98-f416-4dbc-b049-513337bbc4c6'\n" +
                "        ) AS dept\n" +
                "        LEFT JOIN (\n" +
                "        SELECT\n" +
                "        plan.c_company_id,\n" +
                "        count( 1 ) AS num\n" +
                "        FROM\n" +
                "        t_plan plan\n" +
                "        WHERE\n" +
                "        plan.c_db_status = 0\n" +
                "        AND plan.c_maintain_type = 'd10e395c-e9b4-4d63-adeb-623a15301943'\n" +
                "        AND plan.c_state != 1\n" +
                (isSum ? "AND plan.c_plan_state = 2\n" : "") +
                "        GROUP BY\n" +
                "        plan.c_company_id\n" +
                "        ) AS number ON dept.c_dept_id = number.c_company_id";
        Value<List<PestAnalyzeVO>> result = new Value<>();
        run(sql, CommandType.Text, dbCmd -> {
            result.data = dbCmd.executeToList(PestAnalyzeVO.class);
        });
        return result.data;
    }

    @Override
    public List<PestAnalyzeVO> getPestAreaInfo(boolean isSum) {
        String sql = "SELECT\n" +
                "        map.c_map_id AS mapId,\n" +
                "        map.c_name AS mapName,\n" +
                "        IFNULL( number.num, 0 ) AS number\n" +
                "        FROM\n" +
                "        ( select\n" +
                "        map.c_map_id, map.c_name from t_sys_map map where map.c_map_id = 'f41889ad-585b-413d-878e-7040991f468d' union SELECT map.c_map_id, map.c_name FROM t_sys_map map WHERE map.c_map_type = '13002' AND map.c_db_status = 0 ) AS\n" +
                "        map\n" +
                "        LEFT JOIN (\n" +
                "        SELECT\n" +
                "        plan.c_map_id,\n" +
                "        count( 1 ) AS num\n" +
                "        FROM\n" +
                "        t_plan plan\n" +
                "        WHERE\n" +
                "        plan.c_db_status = 0\n" +
                "        AND plan.c_maintain_type = 'd10e395c-e9b4-4d63-adeb-623a15301943'\n" +
                "        AND plan.c_state != 1\n" +
                (isSum ? "AND plan.c_plan_state = 2\n" : "") +
                "        GROUP BY\n" +
                "        plan.c_map_id\n" +
                "        ) AS number ON map.c_map_id = number.c_map_id";
        Value<List<PestAnalyzeVO>> result = new Value<>();
        run(sql, CommandType.Text, dbCmd -> {
            result.data = dbCmd.executeToList(PestAnalyzeVO.class);
        });
        return result.data;
    }

    @Override
    public List<Plan> getScheduleRecord(String uuid, Integer type) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_plan plan").setColumns("plan.*");
        sqlQueryMate.where(type != null, "plan.c_obj_type", ScriptConditionEnum.Eq, type)
                .where(true, "plan.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "plan.c_state", ScriptConditionEnum.GreaterThan, 0)
                .where(true, "plan.c_plan_state", ScriptConditionEnum.GreaterThan, 0)
                .where(StringUtils.isNotEmpty(uuid), "plan.c_obj_id", ScriptConditionEnum.Like, uuid);
        return selectList(sqlQueryMate, Plan.class);
    }

    public List<Map<String, Object>> getNotifyMsg() {
        return getMap("select ifnull(sum(value),0) value from (select count(*) value from t_plan where c_send =1 group by c_plan_id) t");
    }

    @Override
    public DataPager<PlanVO> getTreePlanList(PlanQuery query) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_plan tp").setColumns("tp.*");
        sqlQueryMate.where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tp.c_obj_type", ScriptConditionEnum.Eq, 2);
        return getDataPager(sqlQueryMate, query.pageSize, query.pageIndex - 1, PlanVO.class);
    }

    @Override
    public void updateExecuteDate(ScheduleDTO dto) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_plan tp");
        sqlUpdateMate.setColumnValue(dto.getEndDate() != null, "c_exec_date", dto.getEndDate())
                .setColumnValue(StringUtils.isNotBlank(dto.getCuringDesc()), "c_exec_desc", dto.getCuringDesc())
                .where(true, "c_plan_id", ScriptConditionEnum.Eq, dto.getScheduleId());
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public Plan getPlanInfo(Integer id) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_plan tp left join t_schedule ts on ts.c_schedule_id=tp.c_plan_id").setColumns("tp.*, ts.c_use_pay usePay");
        sqlQueryMate.where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tp.c_id", ScriptConditionEnum.Eq, id);
        return selectOne(sqlQueryMate, Plan.class);
    }

    @Override
    public List<Map<String, Object>> planCount() {
        String sql = "SELECT\n" +
                "  c_maintain_type maintainType,\n" +
                "  c_maintain_type_name maintainTypeName,\n" +
                "  count(*) planCount,\n" +
                "  sum( CASE WHEN c_state = 3 THEN 1 ELSE 0 END ) completeCount,\n" +
                "  ROUND( sum( CASE WHEN c_state = 3 THEN 1 ELSE 0 END )/ count(*) * 100, 2 ) completeRate \n" +
                "FROM t_plan \n" +
                "GROUP BY maintainType, maintainTypeName";
        return getMap(sql);
    }

    @Override
    public DataPager<PlanVO> getSendPlanPage(PlanQuery query, SysUser user, List<String> receiveDeptIds) {
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;
        StringBuilder sqlBuilder = new StringBuilder("(SELECT\n" +
                "\t* \n" +
                "FROM\n" +
                "\tt_plan \n" +
                "WHERE\n" +
                "\tc_db_status = 0 \n" +
                "\tAND c_send = 1 \n" +
                "\tAND c_send_type = 0 \n");
        if (user.userIsAdmin != 1 && CollectionUtils.isNotEmpty(receiveDeptIds)) {
            String deptIdStr = StringUtils.join(receiveDeptIds.stream().map(e -> "'" + e + "'").collect(Collectors.toList()), ",");
            sqlBuilder.append("AND c_send_obj IN (" + deptIdStr + ")\n");
        }
        sqlBuilder.append("UNION SELECT * FROM\n" +
                "\tt_plan \n" +
                "WHERE\n" +
                "\tc_db_status = 0 \n" +
                "\tAND c_send = 1 \n" +
                "\tAND c_send_type = 1 \n");
        if (user.userIsAdmin != 1) {
            sqlBuilder.append("AND c_send_obj ='" + user.userId + "'\n");
        }
        sqlBuilder.append("order by c_db_create_date desc) t");
        String sql = DbScriptHelper.getSelectSqlPage(sqlBuilder.toString(), null, null, null, pageSize, pageIndex);
        DataPager<PlanVO> dataPagerByCustomize = getDataPagerByCustomize(sql, CommandType.Text, null, PlanVO.class);
        return dataPagerByCustomize;
    }

}
